#!/usr/bin/lua

require 'socket'
require 'copas'


local port=23

database = {}


server = socket.bind('*', port)



function handler(socket)
	print('new node')
    socket = copas.wrap(socket)
    while true do
        local data = socket:receive()
        if not data then
            break
        end

		local head=string.match(data, '%b{}')
        if head then
			print('head:',head)
			local headfunc=loadstring('return '..head)
			if headfunc then
				local headtable=headfunc()
				print(headtable)
				if headtable.cmd=='hi' then
					socket:send("hi~"..'\r\n')
				else


				end
			else
				--head formmat error
				print('head formmat error',head)

			end


            socket:send("received head" .. '\r\n')
        end
    end
end



copas.addserver(server, handler)
copas.loop()

